Skip to content

gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576

Open
NaveenKumarG-dev wants to merge 5 commits into
python:mainfrom
NaveenKumarG-dev:gh-mimetypes-guess-type-deprecation
Open

gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576
NaveenKumarG-dev wants to merge 5 commits into
python:mainfrom
NaveenKumarG-dev:gh-mimetypes-guess-type-deprecation

Conversation

@NaveenKumarG-dev

@NaveenKumarG-dev NaveenKumarG-dev commented Jun 17, 2026

Copy link
Copy Markdown

Context

mimetypes.guess_type() is intended for URLs, while mimetypes.guess_file_type() was introduced in Python 3.13 as the preferred API for file paths.

The implementation still accepts file paths without warning, despite the existing # TODO: Deprecate accepting file paths (in particular path-like objects) comment in Lib/mimetypes.py. This PR implements that TODO by deprecating file path inputs to guess_type() and directing users to guess_file_type() instead.

Changes Made

  • Implementation: Modified mimetypes.MimeTypes.guess_type() to emit a DeprecationWarning (stacklevel=2) when called with a file path instead of a URL.

  • Coverage: The warning triggers for plain strings that do not contain a valid URL scheme, as well as for bytes and os.PathLike objects. Valid URLs such as http://, ftp://, and data: continue to behave as before.

  • Testing: Added GuessTypeDeprecationTestCase to Lib/test/test_mimetypes.py covering path and URL inputs to verify warning behavior and prevent regressions.

  • Documentation:

    • Updated Doc/library/mimetypes.rst to document the runtime deprecation.
    • Added entries to Doc/whatsnew/3.16.rst and Doc/deprecations/pending-removal-in-future.rst.
  • NEWS: Added the required NEWS entry using blurb.

Rationale

This change aligns the runtime behavior with the documented distinction between URL handling (guess_type()) and file-path handling (guess_file_type()), while implementing the existing deprecation TODO in the source code.

Linked Issue

Fixes #151575


Code of Conduct and CLA

  • I have read the Python Developer's Guide
  • I have read the Python Code of Conduct
  • I have signed the Contributor License Agreement (CLA)

@read-the-docs-community

read-the-docs-community Bot commented Jun 17, 2026

Copy link
Copy Markdown

@NaveenKumarG-dev NaveenKumarG-dev changed the title gh-151575: Emit DeprecationWarning in mimetypes.guess_type() for file… gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type() Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate passing file paths instead of URLs to mimetypes.guess_type()

1 participant